/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import model.BillDetailModel;
import model.ImportExportBillModel;
import util.DataUtil;

/**
 *
 * @author ThanhToan
 */
public class ImportExportBillDAO {
    public static boolean insertExportBill(ImportExportBillModel obj){
         boolean result = false;
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_insertExportBill(?,?,?,?,?)}");
            csmt.setString("cusID", obj.getCusID());
            csmt.setString("totalPrice", obj.getTotalPrice());
            csmt.setString("discount", obj.getDiscount());
            csmt.setString("orderID", obj.getOrderID());
            csmt.registerOutParameter("id", java.sql.Types.INTEGER);
            result = DataUtil.executeNonStore(csmt);
            if(result){
                String id = csmt.getString("id");
                ArrayList<BillDetailModel> billList = obj.getBillDetail();
                ArrayList<BillDetailModel> billListNew = new ArrayList<BillDetailModel>();
                for(BillDetailModel billModel : billList){
                    billModel.setBillID(id);
                    if(BillDetailDAO.insertExportBill(billModel)){
                        billListNew.add(billModel);
                    }
                }
                obj.setBillDetail(billListNew);
            }
         } catch (Exception e) {
             e.printStackTrace();
             result = false;
         }
         return result;
     }

    public static ArrayList<ImportExportBillModel> getAllImportExportBill(){
         ArrayList<ImportExportBillModel> billList  = new ArrayList<ImportExportBillModel>();
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_getAllImportExportBill()}");
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                ImportExportBillModel obj = new ImportExportBillModel();
                obj.setId(rs.getString("ID"));
                obj.setCusID(rs.getString("CustomerID"));
                obj.setDate(rs.getString("Date"));
                obj.setStatus(rs.getString("Status"));
                obj.setApprovedBy(rs.getString("ApprovedByUser"));
                obj.setTotalPrice(rs.getString("TotalPrice"));
                obj.setDiscount(rs.getString("Discount"));
                obj.setLastUpdate(rs.getString("LastUpdate"));
                obj.setType(rs.getString("Type"));
                obj.setCusName(rs.getNString("CusName"));
                obj.setApprovedName(rs.getNString("ApprovedName"));
                obj.setRejectReason(rs.getNString("RejectReason"));
                obj.setOrderID(rs.getString("OrderID"));
                billList.add(obj);
            }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return billList;
     }

    public static boolean reviewIEBill(ImportExportBillModel obj){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_reviewIEBill(?,?,?,?,?)}");
            csmt.setString("id", obj.getId());
            csmt.setString("status", obj.getStatus());
            csmt.setString("approvedID", obj.getApprovedBy());
            csmt.setNString("reason", obj.getRejectReason());
            csmt.setString("orderID", obj.getOrderID());
            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static ImportExportBillModel getIEBillByID(String id){
         ImportExportBillModel obj = new ImportExportBillModel();
         try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_getImportExportBillByID(?)}");
            csmt.setString("id", id);
            ResultSet rs = DataUtil.executeStore(csmt);
            if(rs.next()){
                obj.setId(rs.getString("ID"));
                obj.setCusID(rs.getString("CustomerID"));
                obj.setDate(rs.getString("Date"));
                obj.setStatus(rs.getString("Status"));
                obj.setApprovedBy(rs.getString("ApprovedByUser"));
                obj.setTotalPrice(rs.getString("TotalPrice"));
                obj.setDiscount(rs.getString("Discount"));
                obj.setLastUpdate(rs.getString("LastUpdate"));
                obj.setType(rs.getString("Type"));
                obj.setCusName(rs.getNString("CusName"));
                obj.setApprovedName(rs.getNString("ApprovedName"));
                obj.setRejectReason(rs.getNString("RejectReason"));
                obj.setOrderID(rs.getString("OrderID"));
            }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return obj;
     }
}
